

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="运行环境 Android Studio 2020.3.1 @[toc]Android Studio 2020.3.1 实现效果 添加景区 方式一添加景区 方式二修改景区 删除景区初始化景区列表 1. UI布局分析 项目结构  实现效果  1.1 主界面 ListView() + ContextMenu + OptionsMenu  ListView组件（ImageView组件 + TextView">
<meta property="og:type" content="article">
<meta property="og:title" content="Android _ 基于Menu组件实现景区的添加、修改、删除">
<meta property="og:url" content="http://example.com/2021/10/26/Android%20_%20%E5%9F%BA%E4%BA%8EMenu%E7%BB%84%E4%BB%B6%E5%AE%9E%E7%8E%B0%E6%99%AF%E5%8C%BA%E7%9A%84%E6%B7%BB%E5%8A%A0%E3%80%81%E4%BF%AE%E6%94%B9%E3%80%81%E5%88%A0%E9%99%A4/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="运行环境 Android Studio 2020.3.1 @[toc]Android Studio 2020.3.1 实现效果 添加景区 方式一添加景区 方式二修改景区 删除景区初始化景区列表 1. UI布局分析 项目结构  实现效果  1.1 主界面 ListView() + ContextMenu + OptionsMenu  ListView组件（ImageView组件 + TextView">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2c6d7ec9f77b4009a238d73606e40a30.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6de3394a45364e039e65d65a593e098a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b2543c4c41f84dbfb528c37faf077baf.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e614287b778e41d5bebe0e6be3da59ce.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/06e6d48c28a341818a4f62abefb7e65f.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d80716b60d8f44cf84a87800f913e4b1.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/75cff2e282ce4fd48f9a95bc49a0453b.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e37bcc30b657423b9a659226ee52b649.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ccb2736de2f44c2089416b0da6f680b6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e16f90e727ea4f29ba66dcb2fdb0526d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8e59f23e2627427b8f3dfb5e21532b7b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2c390887bdb84cbc97538d589f944917.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/01f6ca95b268494098ca353bc3ab4a70.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/42fe914d078749c69992540c74642935.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fbb8a68600e74c42a260402fd45de8c4.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fd59314393204a9ca58fe37b5bae5134.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/514bbc7447de4f6892232b8db8cdf0fe.png">
<meta property="article:published_time" content="2021-10-26T15:26:56.000Z">
<meta property="article:modified_time" content="2022-08-22T15:42:08.728Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/2c6d7ec9f77b4009a238d73606e40a30.png">
  
  
  
  <title>Android _ 基于Menu组件实现景区的添加、修改、删除 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Android _ 基于Menu组件实现景区的添加、修改、删除"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-10-26 23:26" pubdate>
          2021年10月26日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          17k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          143 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Android _ 基于Menu组件实现景区的添加、修改、删除</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="运行环境"><a href="#运行环境" class="headerlink" title="运行环境"></a>运行环境</h1><hr>
<p>Android Studio 2020.3.1<br><img src="https://img-blog.csdnimg.cn/2c6d7ec9f77b4009a238d73606e40a30.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>@[toc]<br>Android Studio 2020.3.1<br><img src="https://img-blog.csdnimg.cn/6de3394a45364e039e65d65a593e098a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="实现效果"><a href="#实现效果" class="headerlink" title="实现效果"></a>实现效果</h1><hr>
<p><code>添加景区 方式一</code><br><img src="https://img-blog.csdnimg.cn/b2543c4c41f84dbfb528c37faf077baf.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>添加景区 方式二</code><br><img src="https://img-blog.csdnimg.cn/e614287b778e41d5bebe0e6be3da59ce.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>修改景区</code><br><img src="https://img-blog.csdnimg.cn/06e6d48c28a341818a4f62abefb7e65f.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><code>删除景区</code><br><img src="https://img-blog.csdnimg.cn/d80716b60d8f44cf84a87800f913e4b1.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>初始化景区列表</code><br><img src="https://img-blog.csdnimg.cn/75cff2e282ce4fd48f9a95bc49a0453b.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="1-UI布局分析"><a href="#1-UI布局分析" class="headerlink" title="1. UI布局分析"></a>1. UI布局分析</h1><hr>
<p><code>项目结构</code></p>
<p><img src="https://img-blog.csdnimg.cn/e37bcc30b657423b9a659226ee52b649.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><code>实现效果</code></p>
<p><img src="https://img-blog.csdnimg.cn/ccb2736de2f44c2089416b0da6f680b6.png" srcset="/img/loading.gif" lazyload alt="[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8Ecre7wG-1635261484956)(Menu-BigTask.assets/image-20211026210113366.png)]"></p>
<h2 id="1-1-主界面"><a href="#1-1-主界面" class="headerlink" title="1.1 主界面"></a>1.1 主界面</h2><hr>
<p><code>ListView() + ContextMenu + OptionsMenu</code></p>
<blockquote>
<p>ListView组件（ImageView组件 + TextView组件）</p>
</blockquote>
<p>列表用于显示景区和对应的景区名。</p>
<blockquote>
<p>ContextMenu组件  </p>
</blockquote>
<p>长按列表弹出一个菜单可进行三个操作<code> 修改 —— 删除 —— 添加</code>（类似于购物平台的购物车功能）</p>
<blockquote>
<p>OptionsMenu组件</p>
</blockquote>
<p>右上角带加号图标的菜单负责弹出添加景区的弹窗，而无需在列表里长按再选择添加，右上角三个点属于同一个菜单，只是不带图标，里面提供一个初始化功能，点击后可将列表内容初始化。</p>
<blockquote>
<p>实现将(图片+名称)的自定义布局放入 ListView 组件的思路</p>
</blockquote>
<p><strong>样式</strong>：创建新的XML布局文件，添加<code>ImageView组件</code>和<code>TextView组件</code>，并调整好位置。</p>
<p><strong>代码</strong>：创建一个景区实体类<code>Landscape.java</code>，包括景区名称和景区图片的ID，新建一个类<code>LandscapeAdapter.java</code>继承<code>ArrayAdapter&lt;Landscape&gt; </code>类，重写<code>getView()</code>方法，用作<code>ListView组件</code>的适配器</p>
<p>getView()方法的作用：</p>
<ul>
<li>根据当前项的位置，从数据列表获取对应的景区对象，更新ImageView组件和TextView组件的信息。</li>
</ul>
<h2 id="1-2-菜单"><a href="#1-2-菜单" class="headerlink" title="1.2 菜单"></a>1.2 菜单</h2><hr>
<p><code>Item </code></p>
<p>菜单无需管理布局，只需要设置是否显示图标之类的。</p>
<h2 id="1-3-弹窗"><a href="#1-3-弹窗" class="headerlink" title="1.3 弹窗"></a>1.3 弹窗</h2><hr>
<p><code>TextView + ImageView + GridView()</code></p>
<p>在长按列表某一项后点击</p>
<blockquote>
<p><code>添加</code>或<code>修改</code>操作后会出现一个<code>弹窗</code> ，弹窗显示的内容为</p>
</blockquote>
<ul>
<li>当前选择的景区信息</li>
<li>可选的所有景区</li>
</ul>
<blockquote>
<p>弹窗关闭时支持的两个选择</p>
</blockquote>
<ul>
<li>OK， 确认当前的景区选择，即更新主页面列表的内容（添加或修改）</li>
<li>Cancel，取消选择</li>
</ul>
<p><code>删除</code> 操作则比较简洁，直接删除列表里当前选择的这项。</p>
<blockquote>
<p>TextView组件 + ImageView组件</p>
</blockquote>
<p>显示当前选择的景区名称以及景区图片</p>
<blockquote>
<p>GridView组件 (ImageView组件 + TextView组件)</p>
</blockquote>
<p>通过网格布局的方式显示所有可选的景区图片以及景区名称</p>
<blockquote>
<p>实现将(图片+名称)的自定义布局放入 GridView 组件的思路</p>
</blockquote>
<p>和之前放入ListView组件的思路一样，同样是用到景区的实体类，接着自定义一个ArrayAdapter的子类，所以可以将其写在<code>LandscapeAdapter.java</code>里，只需要用区分是ListView组件还是GridView组件，它们本质上都是列表组件，只是布局方式不同。</p>
<h2 id="1-4-总结"><a href="#1-4-总结" class="headerlink" title="1.4 总结"></a>1.4 总结</h2><hr>
<table>
<thead>
<tr>
<th>布局的XML文件</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>main.xml</td>
<td>显示主界面，包括两种菜单，当前选择的景区列表</td>
</tr>
<tr>
<td>list_row_view.xml</td>
<td>用于显示主界面景区列表的某一项，包括景区图片和景区名称</td>
</tr>
<tr>
<td>dialog_view.xml</td>
<td>显示弹窗的界面，包括景区名称，景区图片，所有可选择的景区列表</td>
</tr>
<tr>
<td>grid_row_viw.xml</td>
<td>显示弹窗里每个景区项目的信息，包括当前景区的图片和景区名称</td>
</tr>
</tbody></table>
<p><code>layout_main.xml </code></p>
<p>主界面 （右上角弹窗由代码实现）</p>
<p><img src="https://img-blog.csdnimg.cn/e16f90e727ea4f29ba66dcb2fdb0526d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span> <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;ID Name&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ListView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/main_listview&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>



<p><code>list_row_view.xml</code></p>
<p> 主界面景区列表的某一项布局（图片+名称）用于ListView组件</p>
<p><img src="https://img-blog.csdnimg.cn/8e59f23e2627427b8f3dfb5e21532b7b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;horizontal&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:padding</span>=<span class="hljs-string">&quot;5dp&quot;</span></span><br><span class="hljs-tag">    &gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_iv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/bj_beihai&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_tv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:paddingLeft</span>=<span class="hljs-string">&quot;10dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">&quot;1&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>



<p><code>grid_row_view.xml</code></p>
<p> 弹窗点击修改or添加后界面的景区列表的某一项布局 (图片 + 名称) 用于GridView组件</p>
<p><img src="https://img-blog.csdnimg.cn/2c390887bdb84cbc97538d589f944917.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">    &gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_iv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/bj_beihai&quot;</span></span><br><span class="hljs-tag">        /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/row_view_tv&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;TextView&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>



<p><code>dialog_view.xml </code></p>
<p>弹窗点击修改or添加后主界面的布局</p>
<p><img src="https://img-blog.csdnimg.cn/01f6ca95b268494098ca353bc3ab4a70.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;vertical&quot;</span> <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">&quot;horizontal&quot;</span>&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">EditText</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/dialog_editText&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">&quot;1&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">&quot;20dp&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:paddingLeft</span>=<span class="hljs-string">&quot;10dp&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:inputType</span>=<span class="hljs-string">&quot;textPersonName&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;Name&quot;</span> /&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">ImageView</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/dialog_iv&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;80dp&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">android:scaleType</span>=<span class="hljs-string">&quot;centerCrop&quot;</span></span><br><span class="hljs-tag">            <span class="hljs-attr">app:srcCompat</span>=<span class="hljs-string">&quot;@drawable/bj_beihai&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">GridView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/dialog_gridView&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">&quot;1&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:numColumns</span>=<span class="hljs-string">&quot;2&quot;</span></span><br><span class="hljs-tag">        /&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>



<p><code>ContextMenu和OptionsMenu组件的配置</code></p>
<p>和样式XML文件不同，这两个文件无需管布局，有相应的<code>&lt;item&gt;</code>对应Id和提示Title即可。</p>
<p>（OptionsMenu的添加项设置Android自带的icon图片可以更加直观）</p>
<p><code>ctx_menu.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">menu</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">item</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/ctx_add&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:title</span>=<span class="hljs-string">&quot;添加&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">item</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/ctx_modify&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:title</span>=<span class="hljs-string">&quot;修改&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">item</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/ctx_delete&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:title</span>=<span class="hljs-string">&quot;删除&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">menu</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><code>opt_menu.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">menu</span> <span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">item</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/opt_reset&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:title</span>=<span class="hljs-string">&quot;初始化&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">item</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/opt_add&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:icon</span>=<span class="hljs-string">&quot;@android:drawable/ic_input_add&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:title</span>=<span class="hljs-string">&quot;添加&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">app:showAsAction</span>=<span class="hljs-string">&quot;always&quot;</span> /&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">menu</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/42fe914d078749c69992540c74642935.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="2-代码实现"><a href="#2-代码实现" class="headerlink" title="2. 代码实现"></a>2. 代码实现</h1><hr>
<p>项目结构</p>
<p><img src="https://img-blog.csdnimg.cn/fbb8a68600e74c42a260402fd45de8c4.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="2-1-景区实体类"><a href="#2-1-景区实体类" class="headerlink" title="2.1 景区实体类"></a>2.1 景区实体类</h2><hr>
<p><strong>Landscape.java</strong></p>
<p><img src="https://img-blog.csdnimg.cn/fd59314393204a9ca58fe37b5bae5134.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/514bbc7447de4f6892232b8db8cdf0fe.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>主界面 以及 弹窗界面都有显示景区信息，其中包括图片和名称。</p>
<p>最好用一个类来表示一个景区的信息，只需包括景区名和图片ID这两个成员变量，以及对应的构造函数和get、set方法。这样方便组件直接读取景区数据。</p>
<p><code>Landscape.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Landscape</span> &#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> picId;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">Landscape</span><span class="hljs-params">(String name, <span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">getName</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setName</span><span class="hljs-params">(String name)</span> &#123;<br>        <span class="hljs-built_in">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">getPicId</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> picId;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">setPicId</span><span class="hljs-params">(<span class="hljs-type">int</span> picId)</span> &#123;<br>        <span class="hljs-built_in">this</span>.picId = picId;<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>



<h2 id="2-2-列表适配器子类"><a href="#2-2-列表适配器子类" class="headerlink" title="2.2 列表适配器子类"></a>2.2 列表适配器子类</h2><hr>
<p><strong>LandscapeAdapter.java</strong></p>
<p>适配器Adapter是将数据绑定到UI界面上的桥接类。详情可参考 <a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/120934756?spm=1001.2014.3001.5501">【Android】第2章 | ListView &amp; Adapter | 知识点总结 | 包含Demo </a></p>
<p>对景区的增添和删除本质上都是对适配器Adapter的数据进行修改</p>
<p>修改后调用Adapter的<code>notifyDataSetChanged()</code> 方法 即可刷新组件显示的内容</p>
<p>因为显示景区信息的网格列表和主界面的单行列表都是用<code>Landscape.java</code> 景区实体类，所以可并写到同一个类。</p>
<p>区别在于重写实现<code>getView()</code>方法时，对 <code>View</code> 组件的填充选择的布局不同。</p>
<p>主界面景区信息的布局用到了<code>list_row_view.xml</code> </p>
<p>而弹窗景区信息用到的是<code>grid_row_view.xml</code></p>
<p>本质上只是显示效果不同，弹窗的景区信息是呈两列的显示。</p>
<p><code>getView()</code> 方法里一般需调用方法<code>LayoutInflater.from(context).inflate(R.layout.布局XML文件, null, false);</code></p>
<p>将XML布局文件转化成Java的View对象，接着通过该View对象获取到对应布局文件里的组件比如<code>ImageView组件</code> 等</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">import</span> android.content.Context;<br><span class="hljs-keyword">import</span> android.view.LayoutInflater;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.view.ViewGroup;<br><span class="hljs-keyword">import</span> android.widget.ArrayAdapter;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><span class="hljs-keyword">import</span> android.widget.TextView;<br><br><span class="hljs-keyword">import</span> androidx.annotation.NonNull;<br><span class="hljs-keyword">import</span> androidx.annotation.Nullable;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LandscapeAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ArrayAdapter</span>&lt;Landscape&gt; &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">TYPE_GRID_VIEW</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">TYPE_LIST_VIEW</span> <span class="hljs-operator">=</span> <span class="hljs-number">2</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> <span class="hljs-variable">type</span> <span class="hljs-operator">=</span> TYPE_LIST_VIEW;<br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> ArrayList&lt;Landscape&gt; list;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LandscapeAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context,ArrayList&lt;Landscape&gt; list)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LandscapeAdapter</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> Context context,ArrayList&lt;Landscape&gt; list, <span class="hljs-type">int</span> type)</span> &#123;<br>        <span class="hljs-built_in">super</span>(context, android.R.layout.simple_list_item_1, list);<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.list = list;<br>        <span class="hljs-built_in">this</span>.type = type;<br>    &#125;<br>    <span class="hljs-meta">@NonNull</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> View <span class="hljs-title function_">getView</span><span class="hljs-params">(<span class="hljs-type">int</span> position, <span class="hljs-meta">@Nullable</span> View convertView, <span class="hljs-meta">@NonNull</span> ViewGroup parent)</span> &#123;<br>        View v;<br>        <span class="hljs-keyword">if</span> (type == TYPE_LIST_VIEW)&#123;<br>            v = LayoutInflater.from(context).inflate(R.layout.list_row_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        &#125;<span class="hljs-keyword">else</span> &#123;<br>            v = LayoutInflater.from(context).inflate(R .layout.grid_row_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        &#125;<br>        <span class="hljs-type">Landscape</span> <span class="hljs-variable">landscape</span> <span class="hljs-operator">=</span> list.get(position);<br>        <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_iv);<br>        <span class="hljs-type">TextView</span> <span class="hljs-variable">tv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.row_view_tv);<br>        iv.setImageResource(landscape.getPicId());<br>        tv.setText(landscape.getName());<br>        <span class="hljs-keyword">return</span> v;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>



<h2 id="2-3-数据类"><a href="#2-3-数据类" class="headerlink" title="2.3 数据类"></a>2.3 数据类</h2><hr>
<p><strong>LandscapeDataCenter.java</strong></p>
<p>该类只需提供一个获取ArrayList型数据的静态方法，方便在其他类中调用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LandscapeDataCenter</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ArrayList&lt;Landscape&gt; allData;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ArrayList&lt;Landscape&gt; <span class="hljs-title function_">getAllData</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">if</span>(allData == <span class="hljs-literal">null</span>) &#123;<br>            allData = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        &#125;<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;北海&quot;</span>, R.drawable.bj_beihai));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;天安门&quot;</span>, R.drawable.bj_tiananmen));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;天坛&quot;</span>, R.drawable.bj_tiantan));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;花果园&quot;</span>, R.drawable.gy_huaguoyuan));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;甲秀楼&quot;</span>, R.drawable.gy_jiaxiulou));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;雷峰塔&quot;</span>, R.drawable.hz_leifengta));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;千岛湖&quot;</span>, R.drawable.hz_qiandaohu));<br>            allData.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;西湖&quot;</span>, R.drawable.hz_xihu));<br>        <span class="hljs-keyword">return</span> allData;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h2 id="2-4-弹窗类"><a href="#2-4-弹窗类" class="headerlink" title="2.4 弹窗类"></a>2.4 弹窗类</h2><hr>
<p><strong>LandscapeDialog.java</strong></p>
<p>这个类内容比较多，需要慢慢消化。</p>
<p>关于 成员变量</p>
<ul>
<li>context 弹窗的载体，应用上下文，表示当前的App对象 ， 即AppCompatActivity的子类</li>
<li>title 弹窗的标题</li>
</ul>
<p>关于 接口 <code>OnDialogSubmitListener</code></p>
<ul>
<li><p>作用：方法里出现该接口时，使用必须实现的方法onSubmit(Landscape updateData) </p>
<p>在进行添加或修改景区信息操作时，若点击确认，则用于更新信息，具体实现由调用方法时的匿名接口实现</p>
</li>
</ul>
<p>关于方法 showDialog (Landscape landscape, fianl OnDialogSubmitListener l )</p>
<ul>
<li><p>实现点击主页面景区列表某一个项后的弹窗界面</p>
</li>
<li><p>landscape参数表示在主界面列表里选择的景区项</p>
</li>
<li><p>自定义接口OnDialogSubmitlistner用于实现数据更改时的对应操作</p>
</li>
<li><p>具体实现步骤：</p>
</li>
</ul>
<ol>
<li>创建AlertDialog.Builder 对象，（简称为ab），传入参数为context</li>
<li>设置ab标题</li>
<li>根据dialog_view.xml布局文件创建View对象</li>
<li>获取弹窗布局的GridView组件</li>
<li>判断景区是否为空，即可能是由菜单的添加点开的弹窗，或是在空白处点开的弹窗，若为空，则赋予一个默认值，为其创建Landscape对象</li>
<li>根据landscape对象，设置组件的对应信息，EditView显示景区名称，ImageView显示景区图片</li>
<li>使用LandscapeDataCenter类的方法获取所有景区数据，使用该数据创建GridView组件的Adapter对象（之前设计的LandscapeAdapter.java类）</li>
<li>GridView组件设置Adapter，用于展示所有景区的图片和名称</li>
<li>GridView组件设置某项单机时的响应事件，在选择某个景区时，上面的两个组件需要更新信息</li>
<li>ab调用setView方法设置View，并设置确认按钮，第一个参数为按钮标题，第二个内容为响应事件，相应的是获取当前选择的景区信息，创建对象，并调用onSubmit方法，该方法内容由调用showDialog()方法决定。</li>
<li>最后由ab对象的create方法创建一个dialog再调用show方法显示在应用。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">import</span> android.app.AlertDialog;<br><span class="hljs-keyword">import</span> android.content.Context;<br><span class="hljs-keyword">import</span> android.content.DialogInterface;<br><span class="hljs-keyword">import</span> android.view.LayoutInflater;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.EditText;<br><span class="hljs-keyword">import</span> android.widget.GridView;<br><span class="hljs-keyword">import</span> android.widget.ImageView;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LandscapeDialog</span> &#123;<br>    <span class="hljs-keyword">private</span> Context context;<br>    <span class="hljs-keyword">private</span> String title;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">OnDialogSubmitListener</span>&#123;<br>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onSubmit</span><span class="hljs-params">(Landscape updatedData)</span>;<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">LandscapeDialog</span><span class="hljs-params">(Context context, String title)</span> &#123;<br>        <span class="hljs-built_in">this</span>.context = context;<br>        <span class="hljs-built_in">this</span>.title = title;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">showDialog</span><span class="hljs-params">(Landscape landscape, <span class="hljs-keyword">final</span> OnDialogSubmitListener l)</span>&#123;<br>        AlertDialog.<span class="hljs-type">Builder</span> <span class="hljs-variable">builder</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">AlertDialog</span>.Builder(context);<br>        builder.setTitle(title);<br>        <span class="hljs-type">View</span> <span class="hljs-variable">v</span> <span class="hljs-operator">=</span> LayoutInflater.from(context).inflate(R.layout.dialog_view, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>);<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">EditText</span> <span class="hljs-variable">et</span> <span class="hljs-operator">=</span> v.findViewById(R.id.dialog_editText);<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">ImageView</span> <span class="hljs-variable">iv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.dialog_iv);<br>        <span class="hljs-type">GridView</span> <span class="hljs-variable">gv</span> <span class="hljs-operator">=</span> v.findViewById(R.id.dialog_gridView);<br><br>        <span class="hljs-keyword">if</span>(landscape == <span class="hljs-literal">null</span>)&#123;<br>            landscape = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;&quot;</span>, R.mipmap.ic_launcher);<br>        &#125;<br>        et.setText(landscape.getName());<br>        iv.setImageResource(landscape.getPicId());<br>        iv.setTag(landscape.getPicId());<br><br>        <span class="hljs-keyword">final</span> ArrayList&lt;Landscape&gt; allData = LandscapeDataCenter.getAllData();<br>        <span class="hljs-type">LandscapeAdapter</span> <span class="hljs-variable">adapter</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeAdapter</span>(context, allData, LandscapeAdapter.TYPE_GRID_VIEW);<br>        gv.setAdapter(adapter);<br>        gv.setOnItemClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AdapterView</span>.OnItemClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onItemClick</span><span class="hljs-params">(AdapterView&lt;?&gt; adapterView, View view, <span class="hljs-type">int</span> position, <span class="hljs-type">long</span> id)</span> &#123;<br>                <span class="hljs-type">Landscape</span> <span class="hljs-variable">temp</span> <span class="hljs-operator">=</span> allData.get(position);<br>                et.setText(temp.getName());<br>                iv.setImageResource(temp.getPicId());<br>                iv.setTag(temp.getPicId());<br>            &#125;<br>        &#125;);<br>        builder.setView(v);<br>        builder.setPositiveButton(<span class="hljs-string">&quot;确认&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DialogInterface</span>.OnClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(DialogInterface dialogInterface, <span class="hljs-type">int</span> i)</span> &#123;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> et.getText().toString();<br>                <span class="hljs-type">int</span> <span class="hljs-variable">picId</span> <span class="hljs-operator">=</span> (<span class="hljs-type">int</span>) iv.getTag();<br>                <span class="hljs-type">Landscape</span> <span class="hljs-variable">updatedData</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(name, picId);<br>                l.onSubmit(updatedData);<br>            &#125;<br>        &#125;).setNegativeButton(<span class="hljs-string">&quot;取消&quot;</span>, <span class="hljs-literal">null</span>).create().show();<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="2-5-主类"><a href="#2-5-主类" class="headerlink" title="2.5 主类"></a>2.5 主类</h2><hr>
<p><strong>MainAcivity.java</strong></p>
<p>全局成员变量</p>
<ul>
<li><code>ArrayList&lt;Landscape&gt; list</code>  存储主界面显示的列表数据</li>
<li><code>LandscapeAdapter adapter</code>  作为 主界面 ListView  列表 的适配器，用于显示list的数据到组件上</li>
</ul>
<p>所有方法的描述</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">onCreate()</td>
<td align="left">App启动时执行的方法</td>
</tr>
<tr>
<td align="left">addData()</td>
<td align="left">显示添加景区的弹窗</td>
</tr>
<tr>
<td align="left">modifyData()</td>
<td align="left">显示修改景区的弹窗</td>
</tr>
<tr>
<td align="left">resetData()</td>
<td align="left">初始化弹窗里的景区列表信息</td>
</tr>
<tr>
<td align="left">onCreateContextMenu()</td>
<td align="left">长按列表菜单的初始化</td>
</tr>
<tr>
<td align="left">onContextItemSelected()</td>
<td align="left">长按列表菜单的某一项单击时的监听函数</td>
</tr>
<tr>
<td align="left">onCreateOptionsMenu()</td>
<td align="left">右上角菜单的初始化</td>
</tr>
<tr>
<td align="left">onOptionsItemSelected()</td>
<td align="left">右上角菜单某一项单击时的监听函数</td>
</tr>
</tbody></table>
<p>接下来介绍每个方法的具体实现思路及其步骤：</p>
<p><code>onCreate()</code></p>
<p>必备的两句代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>setContentView(R.layout.layout_main); <span class="hljs-comment">// 设置主界面的XML布局文件</span><br></code></pre></td></tr></table></figure>

<ol>
<li>获取主界面的 ListView 组件</li>
<li>创建一个ArrayLIst，插入两个景区信息作为默认的状态</li>
<li>用该ArrayList数据创建LandscapeAdapter对象</li>
<li>给ListView组件设置适配器，这样数据便可以显示在主界面的列表上</li>
<li>给ListView组件注册一个ContextMenu</li>
</ol>
<p><code>addData()</code></p>
<p>该方法主要有两个作用 ， 一是显示一个弹窗，二是在弹窗确认后进行相应的添加操作。</p>
<ol>
<li>创建LandscapeDialog 对象， 用于显示弹窗</li>
<li>调用弹窗对象的showDialog方法，并传入一个匿名接口，用于实现点击确认按钮后添加数据的操作</li>
<li>添加数据，只需修改当前主界面的数据列表list，再更新ListView组件对应的适配器即可。</li>
</ol>
<p><code>modifyData(int pos)</code></p>
<p>该方法用于修改当前主界面列表的某一项</p>
<ol>
<li>通过list数据，以及下表pos获取到需要修改的景区对象</li>
<li>创建LandscapeDialog对象，并调用showDialog方法，实现匿名接口类，直接修改景区对象的名称和图片id，接着再更新适配器。</li>
</ol>
<p><code>resetData() </code></p>
<p>该方法用于初始化主界面的景区信息，这和onCreate方法里的list数据一样，只是恢复到之前插入的状态。</p>
<ol>
<li>清空 list</li>
<li>插入若干个Landscape对象到list</li>
<li>更新适配器</li>
</ol>
<p><code>onCreateContextMenu()</code> </p>
<p>该方法用于App启动时，菜单的界面的初始化，直接配置一下对应的XML文件<code>ctx_menu</code>就好。</p>
<p><code>onContextItemSelected() </code></p>
<p>该方法用于长按主界面列表显示的菜单被单击时的响应事件，负责添加 &#x2F; 修改 &#x2F; 删除，三个操作的逻辑编写。</p>
<ol>
<li>根据菜单响应点击的item对象获取MenuInfo()信息里的当前项下标</li>
<li>使用switch语句判断当前的选择</li>
<li>添加则调用addData() 方法，修改则调用modifyData()方法，并传入下标（表示当前选择的景区所在列表里的下标），删除则直接处理list数据，并更新适配器</li>
<li>最后调用父类的该方法，传入item即可。</li>
</ol>
<p>OptionsMenu的操作和上面类似就不过多解读了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example;<br><br><span class="hljs-keyword">import</span> androidx.annotation.NonNull;<br><span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;<br><br><span class="hljs-keyword">import</span> android.os.Bundle;<br><span class="hljs-keyword">import</span> android.view.ContextMenu;<br><span class="hljs-keyword">import</span> android.view.Menu;<br><span class="hljs-keyword">import</span> android.view.MenuItem;<br><span class="hljs-keyword">import</span> android.view.View;<br><span class="hljs-keyword">import</span> android.widget.AdapterView;<br><span class="hljs-keyword">import</span> android.widget.ListView;<br><br><span class="hljs-keyword">import</span> java.util.ArrayList;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br>    ArrayList&lt;Landscape&gt; list;<br>    LandscapeAdapter adapter;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br>        setContentView(R.layout.layout_main);<br>        <span class="hljs-type">ListView</span> <span class="hljs-variable">lv</span> <span class="hljs-operator">=</span> findViewById(R.id.main_listview);<br>        list = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;北海&quot;</span>, R.drawable.bj_beihai));<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;天安门&quot;</span>, R.drawable.bj_tiananmen));<br>        adapter = <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeAdapter</span>(<span class="hljs-built_in">this</span>, list);<br>        lv.setAdapter(adapter);<br>        registerForContextMenu(lv);     <span class="hljs-comment">// 长按列表后显示Menu</span><br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">addData</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">LandscapeDialog</span> <span class="hljs-variable">dialog</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeDialog</span>(<span class="hljs-built_in">this</span>, <span class="hljs-string">&quot;Add&quot;</span>);<br>        dialog.showDialog(<span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeDialog</span>.OnDialogSubmitListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onSubmit</span><span class="hljs-params">(Landscape updatedData)</span> &#123;<br>                list.add(updatedData);<br>                adapter.notifyDataSetChanged();<br>            &#125;<br>        &#125;);<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">modifyData</span><span class="hljs-params">(<span class="hljs-type">int</span> pos)</span>&#123;<br>        <span class="hljs-keyword">final</span> <span class="hljs-type">Landscape</span> <span class="hljs-variable">landscape</span> <span class="hljs-operator">=</span> list.get(pos);<br>        <span class="hljs-type">LandscapeDialog</span> <span class="hljs-variable">dialog</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeDialog</span>(<span class="hljs-built_in">this</span>, <span class="hljs-string">&quot;Modify&quot;</span>);<br>        dialog.showDialog(landscape, <span class="hljs-keyword">new</span> <span class="hljs-title class_">LandscapeDialog</span>.OnDialogSubmitListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onSubmit</span><span class="hljs-params">(Landscape updatedData)</span> &#123;<br>                landscape.setName(updatedData.getName());<br>                landscape.setPicId(updatedData.getPicId());<br>                adapter.notifyDataSetChanged();<br>            &#125;<br>        &#125;);<br>    &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreateContextMenu</span><span class="hljs-params">(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)</span> &#123;<br>        getMenuInflater().inflate(R.menu.ctx_menu, menu);<br>        <span class="hljs-built_in">super</span>.onCreateContextMenu(menu, v, menuInfo);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">onContextItemSelected</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> MenuItem item)</span> &#123;<br>        <span class="hljs-type">int</span> <span class="hljs-variable">pos</span> <span class="hljs-operator">=</span> ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;<br>        <span class="hljs-keyword">switch</span> (item.getItemId()) &#123;<br>            <span class="hljs-keyword">case</span> R.id.ctx_add:<br>                addData();<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> R.id.ctx_modify:<br>                modifyData(pos);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> R.id.ctx_delete:<br>                list.remove(pos);<br>                adapter.notifyDataSetChanged();<br>                <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.onContextItemSelected(item);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">onCreateOptionsMenu</span><span class="hljs-params">(Menu menu)</span> &#123;<br>        getMenuInflater().inflate(R.menu.opt_menu, menu);<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.onCreateOptionsMenu(menu);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">onOptionsItemSelected</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> MenuItem item)</span> &#123;<br>        <span class="hljs-keyword">switch</span> (item.getItemId()) &#123;<br>            <span class="hljs-keyword">case</span> R.id.opt_add:<br>                addData();<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> R.id.opt_reset:<br>                resetData();<br>                <span class="hljs-keyword">break</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.onOptionsItemSelected(item);<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">resetData</span><span class="hljs-params">()</span> &#123;<br>        list.clear();<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;北海&quot;</span>, R.drawable.bj_beihai));<br>        list.add(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Landscape</span>(<span class="hljs-string">&quot;天安门&quot;</span>, R.drawable.bj_tiananmen));<br>        adapter.notifyDataSetChanged();<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>

<p>虽然是一个小DEMO，但是细节还是挺多的，从布局再到代码部分，如果逻辑没理顺就很容易糊涂，还是得一步一步的来。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%AE%89%E5%8D%93/" class="category-chain-item">安卓</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Android/">#Android</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Android _ 基于Menu组件实现景区的添加、修改、删除</div>
      <div>http://example.com/2021/10/26/Android _ 基于Menu组件实现景区的添加、修改、删除/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年10月26日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/11/02/Android%20%E7%AC%AC3%E7%AB%A0%20Menu%20&amp;%20AlertDialog%20_%20%E7%9F%A5%E8%AF%86%E7%82%B9%E6%B1%87%E6%80%BB%20_%20+Demo/" title="Android 第3章 Menu &amp; AlertDialog _ 知识点汇总 _ +Demo">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Android 第3章 Menu &amp; AlertDialog _ 知识点汇总 _ +Demo</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/25/Android%20_%20%E7%AC%AC2%E7%AB%A0%20_%20ListView%20&amp;%20Adapter%20_%20%E7%9F%A5%E8%AF%86%E7%82%B9%E6%80%BB%E7%BB%93%20_%20+Demo/" title="Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo">
                        <span class="hidden-mobile">Android _ 第2章 _ ListView &amp; Adapter _ 知识点总结 _ +Demo</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
